package com.ocr;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;

import com.ocr.R;
import com.ocr.dao.HistoryDao;
import com.ocr.dao.SettingDao;
import com.ocr.entities.Language;
import com.ocr.entities.LanguageContext;
import com.ocr.entities.SettingItem;
import com.ocr.fragment.FragmentHistory;
import com.ocr.fragment.FragmentResult;
import com.ocr.fragment.FragmentScan;
import com.ocr.fragment.FragmentSetting;
import com.ocr.fragment.FragmentTips;
import com.ocr.navigator.INavigateToFragmentResult;
import com.ocr.utils.ApplicationConstant;

import android.os.Bundle;
import android.os.Environment;
import android.app.Activity;
import android.content.res.AssetManager;
import android.content.res.Resources;
import android.graphics.BitmapFactory;
import android.graphics.drawable.Drawable;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

//import com.googlecode.tesseract.android.TessBaseAPI;

public class MainActivity extends FragmentActivity implements
		INavigateToFragmentResult {
	public static HistoryDao HISTORYDAO;
	private Button btnMenuScan;
	private Button btnMenuHistory;
	private Button btnMenuTip;
	private Button btnMenuSetting;
	public static final String DATA_PATH = Environment
			.getExternalStorageDirectory().toString() + "/OCR/";
	private int currentFragmentID;
	public static Boolean loaded = false;
	public static SettingDao SETTINGDAO;
	public static SettingItem SETTINGITEM;
	public void copyAssetToStoreage(String lang) {
		if (!(new File(DATA_PATH + "tessdata/" + lang + ".traineddata"))
				.exists()) {
			try {

				AssetManager assetManager = getAssets();
				InputStream in = assetManager.open("tessdata/" + lang
						+ ".traineddata");
				OutputStream out = new FileOutputStream(DATA_PATH + "tessdata/"
						+ lang + ".traineddata");
				byte[] buf = new byte[1024];
				int len;
				while ((len = in.read(buf)) > 0) {
					out.write(buf, 0, len);
				}
				in.close();
				out.close();

			} catch (IOException e) {
			}
		}
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		/*
		 * copy tessdata to storeage
		 */
		String[] paths = new String[] { DATA_PATH, DATA_PATH + "tessdata/" };
		for (String path : paths) {
			File dir = new File(path);
			if (!dir.exists()) {
				if (!dir.mkdirs()) {
					Log.d("----copyed", "");
					return;
				} else {

				}
			}

		}
		copyAssetToStoreage(ApplicationConstant.SPA);
		copyAssetToStoreage(ApplicationConstant.FRA);
		copyAssetToStoreage(ApplicationConstant.DEU);
		copyAssetToStoreage(ApplicationConstant.ENG);
		copyAssetToStoreage(ApplicationConstant.VIE);
		copyAssetToStoreage(ApplicationConstant.VIE_ARIAL);
		copyAssetToStoreage(ApplicationConstant.VIE_COURIER_NEW);
		copyAssetToStoreage(ApplicationConstant.VIE_TAHOMA);
		/*
		 * 
		 * 
		 */
		/*
		 * construct list language
		 */
		Language spaLanguage = new Language("Spanish", DATA_PATH, "spa");
		Language fraLanguage = new Language("Frankish", DATA_PATH, "fra");
		Language deLanguage = new Language("German", DATA_PATH, "deu");
		Language engLanguage = new Language("English", DATA_PATH, "eng");
		Language vietLanguage = new Language("Tiếng việt", DATA_PATH,
				ApplicationConstant.VIE_PACKCODE_NORMAL);
		if (loaded == false) {
			LanguageContext.addLanguage(vietLanguage);
			LanguageContext.addLanguage(engLanguage);
			LanguageContext.addLanguage(deLanguage);
			LanguageContext.addLanguage(spaLanguage);
			LanguageContext.addLanguage(fraLanguage);
			loaded = true;
		}
		//setting load
		SETTINGDAO = new SettingDao(getApplicationContext());
		List<SettingItem> listSettingItems = SETTINGDAO.loadAll();
		Log.d(ApplicationConstant.LOGT_TAG, "list setting size:"+listSettingItems.size());
		if(listSettingItems !=null && listSettingItems.size()>0){
			SETTINGITEM = listSettingItems.get(0);
		}
		else
		{
			SETTINGITEM = new SettingItem();
			SETTINGITEM.setAutoProcessImg(false);
			SETTINGITEM.setSelectedLanguagePos(0);
			SETTINGDAO.add(SETTINGITEM);
		}
		// create dao
		HISTORYDAO = new HistoryDao(getApplicationContext());
		// set default fragment
		changeView(1);
		// find view by id
		btnMenuSetting = (Button) findViewById(R.id.menu_setting);
		btnMenuScan = (Button) findViewById(R.id.menu_scan);
		btnMenuHistory = (Button) findViewById(R.id.menu_history);
		btnMenuTip = (Button) findViewById(R.id.menu_tip);
		btnMenuScan.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				changeView(1);
				setSelectedHomeBtn(btnMenuScan);
			}
		});
		btnMenuHistory.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				changeView(2);
				setSelectedHomeBtn(btnMenuHistory);
			}
		});
		btnMenuTip.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				changeView(3);
				setSelectedHomeBtn(btnMenuTip);
			}
		});
		btnMenuSetting.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				changeView(4);
				setSelectedHomeBtn(btnMenuSetting);
			}
		});
	}

	public void setSelectedHomeBtn(Button selectedButton) {
		btnMenuHistory.setBackground(getResources()
				.getDrawable(R.drawable.ocr2));
		btnMenuScan.setBackground(getResources().getDrawable(R.drawable.ocr2));
		btnMenuTip.setBackground(getResources().getDrawable(R.drawable.ocr2));
		btnMenuSetting.setBackground(getResources()
				.getDrawable(R.drawable.ocr2));
		selectedButton.setBackground(getResources()
				.getDrawable(R.drawable.ocr1));
	}

	public void changeView(int view) {
		FragmentManager fragmentManager = getSupportFragmentManager();
		FragmentTransaction fragmentTransaction = fragmentManager
				.beginTransaction();

		switch (view) {
		case 1: {
			FragmentScan fragmentScan = new FragmentScan();
			fragmentTransaction.replace(R.id.content_frame, fragmentScan);
			currentFragmentID = R.layout.fragment_scan;
		}
			break;
		case 2: {
			FragmentHistory fragmentHistory = new FragmentHistory();
			fragmentTransaction.replace(R.id.content_frame, fragmentHistory);
			currentFragmentID = R.layout.fragment_history;
		}
			break;
		case 3: {
			FragmentTips fragmentTips = new FragmentTips();
			fragmentTransaction.replace(R.id.content_frame, fragmentTips);
			currentFragmentID = R.layout.fragment_tips;
		}
			break;
		case 4: {
			FragmentSetting fragmentSetting = new FragmentSetting();
			fragmentTransaction.replace(R.id.content_frame, fragmentSetting);
			currentFragmentID = R.layout.fragment_setting;
		}
			break;
		default:
			break;
		}
		fragmentTransaction.commit();
	}

	@Override
	public void navigateToFragmentResult(String result) {
		// TODO Auto-generated method stub
		FragmentResult fragment = new FragmentResult();
		fragment.navigateToFragmentResult(result);
		FragmentManager fragmentManager = getSupportFragmentManager();
		FragmentTransaction fragmentTransaction = fragmentManager
				.beginTransaction();
		fragmentTransaction.replace(R.id.content_frame, fragment);
		fragmentTransaction.addToBackStack(null);
		fragmentTransaction.commit();
	}
}
